package main

import (
	"fmt"
	"github.com/Shopify/sarama"
)

/*
点对点，发布订阅
广播推送，单个推送，群组推送
异步推送
*/
func main() {
	PushMsg1()
}

// 广播推送
func PushMsg1() {
	// 创建一个config
	addr := []string{"192.168.72.133:9092"}
	config := sarama.NewConfig()
	config.Producer.Return.Successes = true // 同步模式
	config.Version = sarama.V2_1_0_0

	//config.Producer.RequiredAcks = sarama.WaitForAll
	//config.Producer.Partitioner = sarama.NewRandomPartitioner
	//config.Producer.Return.Errors = true

	client, err := sarama.NewClient(addr, config)
	if err != nil {
		fmt.Println("创建Client失败：", err)
	}

	// 创建一个生产者
	producer, err := sarama.NewSyncProducerFromClient(client)
	if err != nil {
		fmt.Println("创建producer失败：", err)
	}

	defer func() {
		if err := producer.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	// 创建一条消息
	//text := "hello kate"
	// producer把消息发给kafka之后不会等待结果返回。
	//producer.Input() <- &sarama.ProducerMessage{Topic: "test", Key: nil, Value: sarama.StringEncoder(text)}
	//// wait response
	//select {
	//case msg := <-producer.Successes():
	////    log.Printf("Produced message successes: [%s]\n",msg.Value)
	//case err := <-producer.Errors():
	//	log.Println("Produced message failure: ", err)
	//default:
	//	log.Println("Produced message default",)
	//}

}
